<template>
  <div class="back">
    <div class="login-container">
      <div class="left-bg">
        <div class="logo"></div>
      </div>
      <div class="right-form">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          autocomplete="off"
          label-position="left"
        >
          <el-form-item prop="mobile">
            <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input v-model="loginForm.mobile" name="mobile" type="text" />
          </el-form-item>

          <el-form-item prop="smsCode" class="smsCode-input">
            <el-input v-model="loginForm.smsCode" name="smsCode" type="text" />
            <div class="smsCode" @click="smsCode">
              <span v-if="!count">获取验证码</span
              ><span v-else>重新获取 {{ count }}s</span>
            </div>
          </el-form-item>

          <el-form-item prop="pwd">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              ref="pwd"
              v-model="loginForm.pwd"
              type="password"
              name="pwd"
            />
          </el-form-item>
          <el-button
            :loading="loading"
            type="primary"
            @click.native.prevent="handleLogin"
            >登录</el-button
          >
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { checkPhoneNumber } from "@/utils/validate";
export default {
  data() {
    return {
      loginForm: {
        mobile: "",
        pwd: "",
        smsType: "SMS_LOGIN",
        smsCode: "",
      },
      loginRules: {
        mobile: [
          {
            required: true,
            validator: checkPhoneNumber,
            trigger: "blur",
          },
        ],
        smsCode: [{ required: true, trigger: "blur", message: "请输入验证码" }],
        pwd: [{ required: true, trigger: "blur", message: "请输入密码" }],
      },
      loading: false,
      count: "",
      timer: null,
      redirect: undefined,
      otherQuery: {},
    };
  },
  // watch: {
  //   $route: {
  //     handler: function (route) {
  //       const query = route.query;
  //       if (query) {
  //         this.redirect = query.redirect;
  //         this.otherQuery = this.getOtherQuery(query);
  //       }
  //     },
  //     immediate: true,
  //   },
  // },
  methods: {
    smsCode() {
      var that = this;
      checkPhoneNumber("", this.loginForm.mobile, function (res) {
        try {
          if (!res) {
            const TIME_COUNT = 60;
            if (!that.timer) {
              const message = that.$message({
                message: "发送中...",
                type: "success",
                duration: 0,
              });
              that.$store
                .dispatch("user/getSmsCode", that.loginForm)
                .then((res) => {
                  const messageTimer = setInterval(() => {
                    message.close();
                    clearInterval(messageTimer);
                  }, 500);
                  if (res.data.resultStatus) {
                    that.count = TIME_COUNT;
                    that.timer = setInterval(() => {
                      if (that.count > 0 && that.count <= TIME_COUNT) {
                        that.count--;
                      } else {
                        clearInterval(that.timer);
                        that.timer = null;
                      }
                    }, 1000);
                  }
                });
            }
          } else {
            throw new Error(res);
          }
        } catch (e) {
          that.$message.error(e.message);
        }
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store.dispatch("user/login", this.loginForm).then((res) => {
            this.loading = false;
            if (res.data.resultStatus) {
              // this.$router.push({
              //   path: this.redirect || "/",
              //   query: this.otherQuery,
              // });
              this.$router.push({
                path:"/userManagement/userList",
              });
            }
          });
        } else {
          return false;
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
  },
};
</script>
<style lang="less" scoped>
.back {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: url("./bg1.jpg") no-repeat;
  background-size: cover;
  .login-container {
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    margin: -380px auto 0 auto;
    max-width: 1200px;
    height: 760px;
    font-size: 0;
    display: flex;
    align-items: center;
    box-shadow: 0px 0px 30px 0px rgba(0, 0, 0, 0.05);
    border-radius: 24px;
    overflow: hidden;
    .left-bg {
      position: relative;
      display: inline-block;
      vertical-align: top;
      width: 546px;
      height: 100%;
      background: url("./bg2.jpg") no-repeat;
      background-size: cover;
      .logo {
        display: block;
        width: 188px;
        height: 87px;
        margin: 320px auto 0 auto;
        background: url("./logo.png") no-repeat;
        background-size: 188px 87px;
      }
    }
    .right-form {
      display: inline-block;
      vertical-align: top;
      width: 654px;
      height: 100%;
      background: #ffffff;
      .el-form {
        padding: 180px 57px 0 57px;
        .el-form-item {
          position: relative;
          padding-bottom: 9px;
          margin-bottom: 30px;
          border-bottom: 1px solid #f4f4f4;
          .svg-container {
            float: left;
            display: inline-block;
            width: 50px;
            height: 50px;
          }
          .smsCode {
            position: absolute;
            right: 0;
            top: 0;
            display: inline-block;
            height: auto;
            line-height: 1;
            font-size: 16px;
            font-weight: 400;
            color: #ffffff;
            padding: 12px 18px;
            background: #011528;
            border-radius: 10px;
            cursor: pointer;
            &:hover {
              opacity: 0.8;
            }
          }
          .el-input {
            padding: 0 50px;
            display: block;
            height: 50px;

            /deep/input {
              border: none;
              height: 50px;
              font-size: 30px;
              font-weight: 500;
            }
          }
          /deep/.el-form-item__error {
            // position: absolute;
            // top: 5px;
            // left: 65px;
            font-size: 30px;
            font-weight: 500;
          }
          &.smsCode-input {
            .el-input {
              padding-left: 0;
            }
            /deep/.el-form-item__error {
              // left: 10px;
            }
          }
        }
        .el-button {
          width: 538px;
          height: 72px;
          margin: 110px auto 0 auto;
          border: none;
          font-size: 30px;
          font-weight: 500;
          color: #ffffff;
          background: #011528;
          border-radius: 45px;
        }
      }
    }
  }
}
</style>
